package cn.edu.sdjzu.xg.kcsj.controller.basic;

import cn.edu.sdjzu.xg.kcsj.domain.Teacher;
import cn.edu.sdjzu.xg.kcsj.service.TeacherService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/insertTeacher.ctl")
public class InsertTeacherController extends HttpServlet {
    @Override
    protected void doPost (HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String teacher_json = JSONUtil.getJSON(req);

        Teacher teacherToAdd = JSON.parseObject(teacher_json, Teacher.class);

        resp.setContentType("application/json;charset=UTF-8");

        JSONObject message = new JSONObject();

        try {
            if (TeacherService.getInstance().add(teacherToAdd)){
                message.put("message", "add yes");
            } else {
                message.put ("message","add no");
            }

        } catch (SQLException e) {
            message.put("message", "sjk have exception");

        } catch (Exception e) {
            message.put("message", "wl have exception");

        }

        resp.getWriter().println(message);
    }
}
